<template>
  <div>
    <Family :activeIndex="'chat'"></Family>
  </div>


  <div style="justify-content: center; display: flex; padding-top: 10px">
    <div
      style="
        width: 980px;
        height: 620px;
        background-color: #f0f0f0;
        border-radius: 2%;
        overflow: hidden;
      "
    >
      <div class="space-top">
        <div class="title">
          <div><span>我的消息</span></div>
          <div><img src="../../../public/image/001.png" alt="" /></div>
        </div>
      </div>
      <div
        class="bottom"
        style="padding-left: 11px; padding-right: 11px; padding-top: 11px"
      >
        <div
          style="
            width: 940px;
            height: 520px;
            padding: 10px;
            background-color: #ffffff;
            border-radius: 2%;
            display: flex;
          "
        >
          <div
            class="lefts"
            style="width: 260px; height: 100%; border-right: 2px solid #f0f0f0"
          >
            <div
              class="titles"
              style="
                width: 100%;
                height: 30px;
                border-bottom: 2px solid #f0f0f0;
                font-size: 14px;
                opacity: 0.6;
              "
              >近期消息</div
            >
            <div class="lists" style="width: 100%; height: 490px">
              <ElScrollbar>
                <div
                  class="list"
                  style="
                    display: flex;
                    padding-left: 20px;
                    align-items: center;
                    cursor: pointer;
                    height: 60px;
                  "
                  v-for="d in chatUser"
                  @click="showInfo(d.user.uid)"
                  :class="{ slist: userId === d.user.uid }"
                >
                  <div class="titles"
                    ><img
                      style="
                        border-radius: 50%;
                        margin-top: 6px;
                        transform: scale(1.4);
                      "
                      :src="d.exinfo.logo ? d.exinfo.logo : defaultImg"
                      alt=""
                  /></div>
                  <div class="names" style="padding-left: 10px">
                    <div>{{ d.user.username }}</div>
                    <div
                      style="
                        font-size: 13px;
                        opacity: 0.6;
                        height: 20px;
                        overflow: hidden;
                      "
                      >{{ d.lastWord }}</div
                    >
                  </div>

                  <div
                    style="position: fixed; padding-left: 10%"
                    v-if="d.unreadCount > 0"
                  >
                    <div
                      style="
                        background-color: crimson;
                        border-radius: 50%;
                        padding: 5px;
                      "
                    >{{ d.unreadCount }}</div>
                  </div>
                </div>
              </ElScrollbar>
            </div>
          </div>
          <div class="right" style="width: 780px; height: 100%">
            <div
              class="right-top"
              style="
                height: 30px;
                display: flex;
                justify-content: center;
                border-bottom: 2px solid #f0f0f0;
              "
            >
              <div>{{ username }}</div>
              <div v-if="username == '请输入私聊的对象的姓名'">
                <ElInput
                  style="width: 110px; height: 25px"
                  v-model="name"
                ></ElInput>
                <ElButton
                  style="width: 70px; height: 25px; margin-left: 15px"
                  type="primary"
                  @click="sendByOther"
                  >确认</ElButton
                >
              </div>
            </div>
            <div
              class="right-content"
              style="
                width: 101.35%;
                height: 340px;
                border-bottom: 1px solid #d8d8d8;
                background-color: #f4f5f7;
                margin: 0;
                padding: 0;
              "
            >
              <!-- 聊天内容 -->
              <ElScrollbar ref="scrollbarRef">
                <div>
                  <div v-for="d in chatContent">
                    <div
                      style="
                        display: flex;
                        justify-content: center;
                        padding-top: 16px;
                        color: #999;
                        margin-bottom: 16px;
                      "
                    >
                      <span>{{ Tools.formatDate(d.sentAt) }}</span>
                    </div>

                    <div
                      v-if="d.senderId == user.data.user.uid"
                      style="
                        display: flex;
                        justify-content: end;
                        padding-right: 16px;
                      "
                    >
                      <div
                        style="
                          background-color: #0084ff;
                          color: white;
                          border-radius: 10px 0 10px 10px;
                          margin: 0 10px 0 0;
                          max-width: 480px;
                        "
                        ><div
                          style="padding: 8px 16px; word-break: break-all"
                          >{{ d.content }}</div
                        ></div
                      >
                      <img
                        style="height: 30px; border-radius: 50%; width: 30px"
                        src="../../../public/image/preview.jpg"
                        alt=""
                      />
                    </div>

                    <div
                      v-if="d.senderId != user.data.user.uid"
                      style="display: flex; padding-left: 16px; margin: 16px 0"
                    >
                      <img
                        style="height: 30px; border-radius: 50%; width: 30px"
                        :src="logo ? logo : defaultImg"
                        alt=""
                      />
                      <div
                        style="
                          background-color: #ffffff;
                          border-radius: 0 10px 10px 10px;
                          margin: 0 0 0 10px;
                          max-width: 480px;
                        "
                        ><div
                          style="padding: 8px 16px; word-break: break-all"
                          >{{ d.content }}</div
                        ></div
                      >
                    </div>
                  </div>
                </div>
              </ElScrollbar>
            </div>
            <div
              class="right-bottom"
              style="
                background-color: #f4f5f7;
                width: 101.35%;
                height: 157px;
                border-radius: 2%;
                z-index: 10px;
              "
            >
              <div class="row" style="display: flex">
                <div
                  class="space-margin"
                  style="margin-right: 16px; padding: 16px 0 10px 16px"
                >
                  <img
                    src="../../../public/image/005.png"
                    alt=""
                    style="cursor: pointer; width: 22px; height: 21px"
                    @click="openFileDialog"
                  />
                </div>
                <div
                  class="space-margin emoji-container"
                  style="margin-right: 16px; padding: 16px 0 0 0"
                >
                  <img
                    src="../../../public/image/006.png"
                    alt=""
                    style="cursor: pointer; width: 22px; height: 21px"
                  />
                </div>
              </div>
              <div
                class="input-box"
                style="width: 100%; height: 60px"
                placeholder="回复一下把"
              >
                <div
                  @keyup="dokeypress"
                  style="height: 60px"
                  contenteditable="true"
                  class="croe-style"
                  id="editableDiv"
                  @input="updateWordCount"
                ></div>
              </div>

              <div
                class="row-right"
                style="
                  float: right;
                  padding-right: 16px;
                  display: flex;
                  padding-top: 10px;
                  color: #c0c0c0;
                  font-size: 12px;
                "
              >
                <div style="margin-right: 25px; margin-top: 5px">
                  <span :class="{ red: wordCount > 500 }">{{ wordCount }}</span
                  >/ <span>500</span>
                </div>
                <ElButton @click="send" :class="{ primary: wordCount > 0 }"
                  >&nbsp;&nbsp;&nbsp;发送&nbsp;&nbsp;&nbsp;</ElButton
                >
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>

  <input
    type="file"
    ref="fileInput"
    style="display: none"
    @change="handleFileChange"
  />
</template>

<script setup>
import { storeToRefs } from 'pinia';
import userstore from '../../store/user';
import {
  ElButton,
  ElInput,
  ElMenu,
  ElMenuItem,
  ElMessage,
  ElScrollbar,
} from 'element-plus';
import { nextTick, onMounted, onUnmounted, ref } from 'vue';
import Family from '../../comment/family.vue';
import Api from '../../script/Api';
import Tools from '../../script/Tools';
import { useRouter, useRoute, onBeforeRouteUpdate } from 'vue-router';

const router = useRouter();
const route = useRoute();
const { user, isLogin } = storeToRefs(userstore());

const defaultImg = ref('../../../public/image/004.png');

const isRead = ref(false);

const wordCount = ref(0);





const fileInput = ref(null);
const files = ref([]);
const openFileDialog = () => {
  fileInput.value.click();
};

const handleFileChange = (e) => {
  files.value = e.target.files[0];
  if (files.value) {
    console.log('所选文件', files.value);
    Api.upload('/files', files.value, {}, (data) => {
      if (data.success) {
        Api.post(
          '/private/send',
          { receiverId: userId.value, content: Api.logo(data.data.id) },
          (data) => {
            console.log('data', data);
            if (data.success) {
              getchatContent(userId.value);
            }
          }
        );
      } else {
        ElMessage.error(data.message);
      }
    });
  } else {
    ElMessage.error(data.message);
  }
};

//#region 根据姓名发布私信
const name = ref('');

const sendByOther = () => {
  Api.post('/private/sendByOther', { username: name.value }, (data) => {
    console.log('username', data);
    if (data.success) {
      name.value = '';
      location.reload();
    } else {
      ElMessage.error(data.message);
    }
  });
};

//#endregion

const updateWordCount = (event) => {
  const target = event.target;
  const text = target.textContent.trim();
  wordCount.value = text.length; // 更新响应式数据的值，视图会自动更新
};

//#region 获取聊天对象
const chatUser = ref([]);
const getChatUser = () => {
  Api.get('/private', {}, (data) => {
    if (data.success) {
      chatUser.value = data.data;
    } else {
      ElMessage.error(data.message);
    }
  });
};

getChatUser();

//#endregion

//#region 获取跟聊天对象的聊天内容
const chatContent = ref([]);

const getchatContent = (id) => {
  Api.get(`/private/${id}`, {}, (data) => {
    if (data.success) {
      console.log('data', data.data);
      chatContent.value = data.data;
        
      Api.get(`/private/read/${userId.value}`,{},(data) => {
        if (data.success) {
            return;
        } 
      })
      
    } else {
      ElMessage.error(data.message);
    }
  });
};

//获取uid的用户角色信息
const userInfo = ref([]);
const username = ref('请输入私聊的对象的姓名');
const logo = ref('');
const userId = ref([]);

const queryByUid = (uid) => {
  Api.get(`/user/queryById/${uid}`, {}, (data) => {
    if (data.success) {
      username.value = data.data.user.username;
      logo.value = data.data.exinfo.logo;
      userId.value = data.data.user.uid;
    } else {
      ElMessage.error(data.message);
    }
  });
};

//#endregion

//#region 发送私信
const content = ref('');

const getContent = () => {
  const editableDiv = document.getElementById('editableDiv');
  content.value = editableDiv.innerText;
};

const clearContent = () => {
  const editableDiv = document.getElementById('editableDiv');

  editableDiv.innerHTML = '';
};

const send = () => {
  getContent();
  Api.post(
    '/private/send',
    { receiverId: userId.value, content: content.value },
    (data) => {
      if (data.success) {
        content.value = '';
        getchatContent(userId.value);
        isRead.value = false;
        clearContent();
      } else {
        ElMessage.error(data.message);
      }
    }
  );
};

const dokeypress = (event) => {
  if (event.key == 'Enter' && event.shiftKey) {
    send();
  }
};

//#endregion

onMounted(() => {
  console.log('页面挂载');
  userId.value = route.params.uid ? route.params.uid : -1;
  if (userId.value == -1) {
    return;
  }
  queryByUid(userId.value);
  getchatContent(userId.value);
});

onBeforeRouteUpdate((to, from) => {
  userId.value = to.params.uid ? to.params.uid : -1;
  if (userId.value == -1) {
    return;
  }
  queryByUid(userId.value);
  getchatContent(userId.value);
});

const showInfo = (id) => {
  // router.push(`/user/test/${id}`);
  router.push(`/user/test/${id}`);
};
</script>

<style scoped>
.red {
  color: red;
}

.primary {
  background-color: #1389bf;
  color: #fff;
}

.space-margin:hover {
  color: #409eff;
}

.croe-style {
  width: 100%;
  min-height: 22px;
  max-height: 300px;
  letter-spacing: 1px;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  white-space: pre-wrap;
  overflow-x: hidden;
  overflow-y: auto;
  color: #222;
  background-color: transparent;
  font-size: 14px;
  vertical-align: baseline;
  outline: none;
  line-height: normal;
  border-radius: 4px;
  word-break: break-word;
  word-wrap: break-word;
  padding-left: 16px;
}

.dialog-title {
  height: 36px;
  border: 1px solid #fff;
}

.close {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 1;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-pack: center;
  -ms-flex-pack: center;
  justify-content: center;
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
  width: 24px;
  -webkit-transform: translateX(-100%);
  transform: translateX(-100%);
  opacity: 0;
  -webkit-transition: 300ms;
  transition: 300ms;

  font: 12px;
  color: #999;
}

.close:hover {
  opacity: 1;
}

.list {
}

.slist {
  background-color: #f0f0f0;
}

.list:hover {
  background-color: #f0f0f0;
}

@keyframes clo {
  25% {
    left: 1;
    opacity: 1;
  }
  100% {
    left: 1;
    opacity: 1;
  }
}

.avatars {
  width: 50px;
  height: 50px;
  border-radius: 50%;
}

.last-word {
  color: #999;
  padding: 8px 0;
  margin-bottom: -6px;
  overflow: hidden;
  width: 155px;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.name {
  color: #333;
  font-size: 14px;
  min-height: 16px;
  line-height: 1;
}

.name-box {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-orient: vertical;
  -webkit-box-direction: normal;
  -ms-flex-direction: column;
  flex-direction: column;
  -webkit-box-pack: center;
  -ms-flex-pack: center;
  justify-content: center;
}

.list-avatar {
  display: block;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  -ms-flex-negative: 0;
  flex-shrink: 0;
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;

  margin-right: 8px;
  position: relative;
}

.list-item {
  width: 100%;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-orient: horizontal;
  -webkit-box-direction: normal;
  -ms-flex-direction: row;
  flex-direction: row;
  padding: 19px 24px;
  position: relative;
  cursor: pointer;
}

.list-container {
}

.ps {
  overflow: hidden !important;
  overflow-anchor: none;
  -ms-overflow-style: none;
  touch-action: auto;
  -ms-touch-action: auto;
}

.left {
  width: 240px;
  border-right: 1px solid #e9eaec;
  position: relative;
  height: 530px;
  overflow-y: scroll;
}
.left-title {
  padding-left: 24px;
  line-height: 35px;
  height: 36px;
  border-bottom: 1px solid #e9eaec;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
  overflow: hidden;
}

.im {
  font-size: 12px;
  color: #666;
  background-color: #fff;
  -webkit-box-shadow: 0 2px 4px 0 rgba(121, 146, 185, 0.54);
  box-shadow: 0 2px 4px 0 rgba(121, 146, 185, 0.54);
  border-radius: 4px;
  height: 100%;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  position: relative;
}
.space-bottom {
  padding-top: 10px;
}

.title {
  height: 42px;
  background-color: #fff;
  -webkit-box-shadow: 0 2px 4px 0 rgba(121, 146, 185, 0.54);
  box-shadow: 0 2px 4px 0 rgba(121, 146, 185, 0.54);
  -ms-flex-negative: 0;
  flex-shrink: 0;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
  -webkit-box-pack: justify;
  -ms-flex-pack: justify;
  justify-content: space-between;
  padding: 0 16px;
  font-size: 15px;
  color: #666;
  border-radius: 4px;
}

.space-top {
  padding: 10px 10px 0;
  z-index: 11;
}

.avatar {
  width: 40px;
  border-radius: 50%;
}
.avatar:hover {
  width: 100px;
}

.el-menu--horizontal > .el-menu-item:nth-child(3) {
  margin-right: auto;
}

#water {
  margin-top: 30px;
  width: 30px;
  height: 30px;

  border-radius: 59% 41% 60% 40% / 49% 42% 58% 51%;
  box-shadow: inset 1px 2px 3px rgba(0, 0, 0, 0.5),
    1px 2px 2px rgba(0, 0, 0, 0.3), 1px 1px 3px rgba(0, 0, 0, 0.05),
    inset -10px -10px 15px rgba(255, 255, 255, 0.8);
  animation: action 3s linear infinite alternate;
}

/* #water::after{
      content: '';
      width: 20px;
      height: 20px;
      position: absolute;
      top: 24px;
      left: 48%;
      background-color: rgba(255, 255, 255, 0.8);
      border-radius: 30% 70% 35% 65% / 28% 44% 56% 72% ;
    }

    #water::before{
      content: '';
      width: 1px;
      height: 1px;
      position: absolute;
      top: 2px;
      left: 44%;
      background-color: rgba(255, 255, 255, 0.8);
      border-radius: 61% 39% 67% 33% / 28% 44% 56% 72% ;
    } */

@keyframes action {
  25% {
    border-radius: 80% 34% 55% 45% / 49% 46% 54% 51%;
  }

  50% {
    border-radius: 61% 39% 50% 50% / 43% 53% 47% 57%;
  }

  100% {
    border-radius: 43% 47% 45% 55% / 47% 55% 45% 53%;
  }
}

.img {
  width: 50px;
  height: 50px;
  display: flex;
  justify-content: center;
  align-items: center;
  border-radius: 20px;
  box-shadow: 2px 2px 30px rgba(0, 0, 0, 0.2),
    -2px -2px 2px rgba(255, 255, 255, 1);
  transition: all 0.2s ease-out;
}

img {
  width: 28px;
  transition: all 0.2s ease-out;
}

.img:hover {
  cursor: pointer;
  box-shadow: 0 0 0 rgba(0, 0, 0, 0.2), 0 0 0 rgba(255, 255, 255, 0.8),
    inset 2px 2px 30px rgba(0, 0, 0, 0.1),
    inset -2px -2px 30px rgba(255, 255, 255, 1);
}

.img:hover img {
  width: 25px;
}
</style>
